package com.example.controller;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.example.annotation.Log;
import com.example.common.result.Result;
import com.example.entity.yifei.Copta;
import com.example.entity.yifei.Coptb;
import com.example.mapper.CoptaMapper;
import com.example.mapper.CoptbMapper;
import com.example.service.CoptaService;
import com.example.service.impl.ActFlowCommService;
import com.example.untils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/CopTa")
@Api(tags = "报价单相关接口")
@Slf4j
public class CoptaController {
    @Resource
    private ActFlowCommService actFlowCommService;

    @Resource
    private CoptaService coptaService;

    @Resource
    private CoptaMapper coptaMapper;

    @Resource
    private CoptbMapper coptbMapper;

    @Log("上报报价单")
    @ApiOperation("上报报价单")
    @GetMapping("/report")
    @DSTransactional
    public Result   report(
            @RequestParam String TC001, @RequestParam String TC002, @RequestParam String username, @RequestParam String leader
    ) {
        if (StringUtils.isEmpty(TC001) || StringUtils.isEmpty(TC002)) {
            return Result.error("单别或单号为空");
        }
        if (StringUtils.isEmpty(username)) {
            return Result.error("用户名为空");
        }
        String formkey = "copta";
        String beanName = formkey + "ServiceImpl";
        ProcessInstance processInstance;
        String bussinessKey = formkey + ":" + TC001 + ":" + TC002;
        String id = TC001 + ":" + TC002;
        Boolean b = actFlowCommService.isOnly(bussinessKey, formkey);
        Boolean b2 = coptaService.tc30IsN(TC001, TC002);
        if (b && b2) {
            processInstance = actFlowCommService.startProcess(formkey, beanName, bussinessKey, id, username, leader);
        } else {
            return Result.error("有相同单据正在审核或该单据已审核完成");
        }
        //获取流程实例id
        String processInstanceId = processInstance.getId();
        List<Task> taskList = actFlowCommService.myTaskList(username);
        if (!CollectionUtils.isEmpty(taskList)) {
            for (Task task : taskList) {
                if (task.getAssignee().equals(username) &&
                        task.getProcessInstanceId().equals(processInstanceId)) {
                    log.info("ProcessInstanceId is {}", task.getProcessInstanceId());
                    log.info("taskid is {}",task.getId());
                    actFlowCommService.completeProcess("同意", username, task.getId(),"");
                }
            }
        }
        else {
            return Result.error("空任务！");
        }
        return Result.success("上报报价单成功");
    }

    @Log("报价单详情")
    @ApiOperation("报价单详情  Copta 报价单单头 Coptb 报价单单身")
    @PostMapping("/queryXq")
    public Result<Map<String,Object>> queryXq(@RequestParam(defaultValue = "") String TC001,
                                              @RequestParam(defaultValue = "") String TC002){

        if (StringUtils.isEmpty(TC001) || StringUtils.isEmpty(TC002)) {
            return Result.error("单别或单号为空");
        }
        Copta copta = coptaMapper.selectOne(new LambdaQueryWrapper<Copta>().eq(Copta::getTA001, TC001).eq(Copta::getTA002, TC002));
        if (copta == null) {
            return Result.error("报价单不存在");
        }
        List<Coptb> coptbList = this.coptbMapper.selectList(new LambdaQueryWrapper<Coptb>().eq(Coptb::getTb001, TC001).eq(Coptb::getTb002, TC002));
        Map<String,Object> map = new HashMap<>();
        map.put("copta",copta);
        map.put("coptbList",coptbList);
        return Result.success(map);
    }

}
